package main.Q1901_2000;

public class Q1901_1910 {
    public static void main(String[] args) {
        System.out.println("Question1901：寻找峰值Ⅱ");
        System.out.println("Question1902：");
        System.out.println("Question1903：");
        System.out.println("Question1904：");
        System.out.println("Question1905：统计子岛屿");
        System.out.println("Question1906：");
        System.out.println("Question1907：");
        System.out.println("Question1908：");
        System.out.println("Question1909：");
        System.out.println("Question1910：删除一个字符串中所有出现的给定子字符串");
    }
}

class Question1901 {
    public int[] findPeakGrid(int[][] mat) {
        int[] result = new int[2];
        for (int i = 0; i < mat.length; i++) {
            boolean flag = false;
            for (int j = 0; j < mat[0].length; j++) {
                boolean flagUp = false, flagDown = false, flagLeft = false, flagRight = false;
                if ((i - 1 >= 0 && mat[i][j] > mat[i - 1][j]) || i - 1 < 0) flagUp = true;//上
                if ((i + 1 < mat.length && mat[i][j] > mat[i + 1][j]) || i + 1 >= mat.length) flagDown = true;//下
                if ((j - 1 >= 0 && mat[i][j] > mat[i][j - 1]) || j - 1 < 0) flagLeft = true;//左
                if ((j + 1 < mat[0].length && mat[i][j] > mat[i][j + 1]) || j + 1 >= mat[0].length) flagRight = true;//右
                if (flagDown && flagRight && flagLeft && flagUp) {
                    result[0] = i;
                    result[1] = j;
                    flag = true;
                }
            }
            if (flag) break;
        }
        return result;
    }
}

class Question1905 {
    int rc;
    int cc;

    public int countSubIslands(int[][] grid1, int[][] grid2) {
        rc = grid1.length;
        cc = grid1[0].length;
        for (int i = 0; i < rc; i++) {
            for (int j = 0; j < cc; j++) {
                if (grid2[i][j] == 1) grid2[i][j] += grid1[i][j];
            }
        }
        int res = 0;
        for (int i = 0; i < rc; i++) {
            for (int j = 0; j < cc; j++) {
                if (grid2[i][j] == 2 && fill(grid2, i, j)) res++;
            }
        }
        return res;
    }

    public boolean fill(int[][] grid, int i, int j) {
        if (i < 0 || i >= rc || j < 0 || j >= cc) return true;
        if (grid[i][j] != 2) return grid[i][j] == 0;
        grid[i][j] = 0;
        boolean down = fill(grid, i - 1, j);
        boolean up = fill(grid, i + 1, j);
        boolean right = fill(grid, i, j - 1);
        boolean left = fill(grid, i, j + 1);
        return down & up & right & left;
    }
//    int count=0;
//    public void process1(int[][] grid,int r,int c,List<int[]> list){
//        int row= grid.length,col=grid[0].length;
//        list.add(new int[]{r,c});
//        grid[r][c]=0;
//        if (r-1>=0&&grid[r-1][c]==1) process1(grid,r-1,c,list);
//        if (r+1<row&&grid[r+1][c]==1) process1(grid,r+1,c,list);
//        if (c-1>=0&&grid[r][c-1]==1) process1(grid,r,c-1,list);
//        if (c+1<col&&grid[r][c+1]==1) process1(grid,r,c+1,list);
//    }
//    public boolean process2(int[][] grid,int r,int c,List<int[]> list){
//        int row= grid.length,col=grid[0].length;
//        boolean up=true,down=true,left=true,right=true;
//        grid[r][c]=0;
//        if (r-1>=0&&grid[r-1][c]==1) {
//            if (!isContains(list,new int[]{r-1,c})||!process2(grid,r-1,c,list)) up=false;
//        }
//        if (r+1<row&&grid[r+1][c]==1) {
//            if (!isContains(list,new int[]{r+1,c})||!process2(grid,r+1,c,list)) down=false;
//        }
//        if (c-1>=0&&grid[r][c-1]==1) {
//            if (!isContains(list,new int[]{r,c-1})||!process2(grid,r,c-1,list)) left=false;
//        }
//        if (c+1<col&&grid[r][c+1]==1){
//            if (!isContains(list,new int[]{r,c+1})||!process2(grid,r,c+1,list)) right=false;
//        }
//        return up&&down&&left&&right;
//    }
//    public boolean isContains(List<int[]> list,int[] cur){
//        for (int[] arr:list){
//            if (arr[0]==cur[0]&&arr[1]==cur[1])
//                return true;
//        }
//        return false;
//    }
//    public int countSubIslands(int[][] grid1, int[][] grid2) {
//        int row=grid1.length,col=grid1[0].length;
//        List<int[]> list=new ArrayList<>();//记录1中所有岛屿
//        for (int i=0;i<row;i++){
//            for (int j=0;j<col;j++){
//                if (grid1[i][j]==1){
//                    process1(grid1,i,j,list);
//                }
//            }
//        }
//        for (int i=0;i<row;i++){
//            for (int j=0;j<col;j++){
//                if (grid2[i][j]==1){
//                    if (isContains(list,new int[]{i,j})&&process2(grid2,i,j,list)) {
//                        count++;
//                    }
//                }
//            }
//        }
//        return count;
//    }
}

class Question1910 {
    public String removeOccurrences(String s, String part) {
        String result = s;
        while (result.contains(part)) {
            int i = result.indexOf(part);
            result = result.substring(0, i) + result.substring(i + part.length(), result.length());
        }
        return result;
    }
}